package com.meiqiongproperty.yiguanjia.fragment.home.health;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.yiHealthyMedicalAdapter.PhysicianConsultListAdapter;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendPhysicianConsult;
import com.meiqiongproperty.yiguanjia.httpbean.healthy.PhysicianConsultBean;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/24.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.health
 */

public class PhysicianConsultFragment extends BaseFragment {
    com.meiqiongproperty.yiguanjia.databinding.PhysicianConsultListLayoutBinding binding;
    PhysicianConsultListAdapter adapter;
    List<PhysicianConsultBean.AsklistBean> ask_list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.physician_consult_list_layout, container, false);
        initView();
        getConsultList();
        return binding.getRoot();
    }

    public void initView(){
        binding.navigation.setTitle("咨询列表");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.navigation.setRightText("我要提问");
        binding.navigation.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GenericFragmentActivity.start(getActivity(), WriteQuestionContentFragment.class, new Bundle());
            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerPhysicianConsult.setLayoutManager(manager);
    }

    /**
     * 获取咨询列表数据
     */
    public void getConsultList() {
        SendPhysicianConsult consult=new SendPhysicianConsult();
        consult.setToken(Utils.getToken(getActivity()));
        consult.setRoomID(Utils.getRoomID(getActivity()));
        consult.setPageNo("1");
        consult.setPageSize("50");

        ApiService.getPhysicianConsultList(getActivity(), new IHttpCallback<PhysicianConsultBean>() {
            @Override
            public void onNext(PhysicianConsultBean physicianConsultBean) {
                ask_list.clear();
                ask_list.addAll(physicianConsultBean.getAsklist());

                adapter = new PhysicianConsultListAdapter(getActivity(), ask_list);
                binding.recyclerPhysicianConsult.setAdapter(adapter);
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //Toast.makeText(getActivity(), "您点击了" + position, Toast.LENGTH_SHORT).show();
                        Bundle bundle=new Bundle();
                        bundle.putString("askId",ask_list.get(position).getAskId());
                        Toast.makeText(getActivity(),ask_list.get(position).getAskId(),Toast.LENGTH_SHORT).show();
                        GenericFragmentActivity.start(getActivity(), PhysicianConsultDetailFragment.class, bundle);
                    }
                });
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(consult));
    }

    @Override
    public void onResume() {
        super.onResume();
        getConsultList();
    }
}
